package cn.org.xwb.bookmall.model.service.impl;

import cn.org.xwb.bookmall.model.po.Orders;
import cn.org.xwb.bookmall.model.mapper.OrdersMapper;
import cn.org.xwb.bookmall.model.service.IOrdersService;
import cn.org.xwb.bookmall.model.utils.BookRedisUtil;
import cn.org.xwb.bookmall.model.vo.OrderVo;
import cn.org.xwb.common.redis.utils.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwb
 * @since 2023-12-06
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private BookRedisUtil bookRedisUtil;

    @Autowired
    private RedisUtil redisUtil;

    private String getKeyOrder() {
        return bookRedisUtil.getRedisKeyUser("order%s", "");
    }

    public List<OrderVo> findAndCacheAll() {
        List<OrderVo> orderVos = baseMapper.selectAll();
        if (Objects.isNull(orderVos)) return null;
        Boolean res = redisUtil.setList(getKeyOrder(), orderVos);
        if (!res) return null;
        return orderVos;
    }

    public List<OrderVo> findFromCache() {
        List<OrderVo> orderVos = (List<OrderVo>) redisUtil.get(getKeyOrder());
        if (Objects.isNull(orderVos)) orderVos = findAndCacheAll();
        return orderVos;
    }
}
